import DataManager from "./DataManager";

const ResourcesManager = {
    /**
         * 
         * @param {*} filename 文件名称
         * @param {*} cb 加载回调
         */
    loadJson(filename, cb) {
        cc.resources.load("config/" + filename, function (err, object) {
            if (err) {
                // console.log("load json failed:" + err);
            } else {
                if (cb) cb(object.json, filename);
            }
        });
    },
    /**
     * 加载Icon
     * @param {*} filename
     * @param {*} cb
     */
    loadAtlas(filename, cb) {
        cc.resources.loadDir(filename, cc.SpriteFrame, function (err, atlas) {
            if (err) {
                // console.log("load SpriteAtlas failed:" + err);
            } else {

                if (cb) cb(atlas, filename);
            }
        });
    },
    /**
     * 批量加载同一文件夹中所有音频文件
     * 
     * @param {*} filename 
     * @param {*} cb 
     */
    loadAudios(filename,cb){
        cc.resources.loadDir(filename, cc.AudioClip, function (err, audios) {
            if (err) {
                // console.log("load Audios failed:" + err);
            } else {
                if (cb) cb(audios, filename);
            }
        });
    },
    
    /**
  * 加载图片
  * @param {*} filename 
  * @param {*} cb 
  */
    loadImg(pathname, cb) {
        cc.resources.load(pathname, cc.SpriteFrame, function (err, SpriteFrame) {
            if (err) {
                // console.log("load SpriteFrame failed:" + err);
            } else {
                if (cb) cb(SpriteFrame)
            }
        });
    },

    load(prefabName, callback, type = cc.Prefab, isCache = true, isInstant = true, root = "prefab/") {
        if (!this.prefabDic) this.prefabDic = {};
        if (this.prefabDic[prefabName]) {
            if (isInstant) {
                var node = cc.instantiate(this.prefabDic[prefabName]);
                if (callback) {
                    callback(node);
                }
            } else {
                if (callback) {
                    callback(this.prefabDic[prefabName]);
                }
            }
            return;
        }
        var path = root + prefabName;
        cc.resources.loadRes(path, type, (error, prefab) => {
            if (!error) {
                if (isCache) {
                    this.prefabDic[prefabName] = prefab;
                }
                if (isInstant) {
                    var node = cc.instantiate(prefab);
                    if (callback) {
                        callback(node);
                    }
                } else {
                    if (callback) {
                        callback(prefab);
                    }
                }
            } else {
                // cc.log("加载资源失败 path", path, error.toString());
            }

        })
    },


    /**
     * 加载并创建指定预制
     * @param {*} bundleName 
     * @param {*} prefabName 
     * @param {*} cb 
     */
    createPrefab(bundleName, prefabName, cb) {
        let bundle = cc.assetManager.getBundle(bundleName);
        if (!bundle) {
            cc.assetManager.loadBundle(bundleName, function (err, bundle) {
                if (err) {
                    console.error("prefab 加载错误：", err);
                    return;
                }
                DataManager.loadPrefab(bundle, prefabName, cb);
            });
        } else {
            DataManager.loadPrefab(bundle, prefabName, cb);
        }
    },

    loadPrefab(bundle, prefabName, cb) {
        let levelPrefab = bundle.get(prefabName);
        if (levelPrefab) {
            if (cb) cb(levelPrefab);
        } else {
            bundle.load(prefabName, cc.Prefab, function (error, prefab) {
                if (error) {
                    cc.error(error);
                    return;
                }
                if (cb) cb(prefab);
            });
        }
    },

    /**从对象池中获取目标对象 */
    getObjByPool(poolName, model) {
        if (!ResourcesManager[poolName]) {
            ResourcesManager[poolName] = new cc.NodePool();
        }
        let _game;
        if (ResourcesManager[poolName].size() > 0) {
            _game = ResourcesManager[poolName].get();
        } else {
            _game = cc.instantiate(model);
        }
        return _game;
    },
    /**放置对象到对象池中 */
    putObjToPool(poolName, gameObj){ 
        ResourcesManager[poolName].put(gameObj);
    },

}

export default ResourcesManager;